package light.head.controller.mobile;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.mobile.MobileTaskDAO;
import light.head.dao.sys.UserInfoDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.Task;
import light.head.model.UserInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;



@Controller
@RequestMapping(Url.MOBILE_TASKOWN)
public class MobileTaskOwnController extends SupporterController {

	private MobileTaskDAO mobileTaskDAO;
	private UserInfoDAO userInfoDAO;
	
	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest reqeust, HttpServletResponse response) {
		return new ModelAndView(Url.MOBILE_TASKOWN_INIT);
	}
	
	@RequestMapping(Url.QUERY)
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) {
		Map<String, String> map = createQueryMapByParmName(request, Parm.CUNAME, Parm.RU, Parm.USER, Parm.TITLE);
		map.put(Parm.USER, cu_code(request).toString());
		map.put(Parm.RU, cu_code(request).toString());
		PageBean pb = createPageBean(request);
		mobileTaskDAO.findByMap(cu_organCode(request), map, pb);
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.MOBILE_TASKOWN_LIST);
	}
	
	@RequestMapping(Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) {
		List<UserInfo> userList = userInfoDAO.findByOrganCode(cu_organCode(request));
		request.setAttribute(Parm.USER, userList);
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			Task task = mobileTaskDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.TASK, task);
		}
		return new ModelAndView(Url.MOBILE_TASKOWN_EDIT);
	}
	
	@RequestMapping(Url.SAVE)
	public void save(Task task, HttpServletRequest request, HttpServletResponse response) {
		RESULT result = RESULT.ERROR;
		if (null == task.getCode()) {
			task.setDeleted(false);
			task.setUser(cu_code(request));
			task.setDu(cu_code(request));
			task.setOc(cu_organCode(request));
			task.setRu(cu_code(request));
			result = mobileTaskDAO.add(task);
		} else {
			result = mobileTaskDAO.upd(task);
		}
		
		if (RESULT.SUCCESS == result) {
			print(response, Msg.SUCCESS);
		} else 
			print(response, Msg.ERROR);
	}
	
	
	@RequestMapping(value = Url.DELETE)
	public void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		Task task = mobileTaskDAO.get(Integer.parseInt(code));
		task.setDeleted(true);
		if (mobileTaskDAO.upd(task) == RESULT.SUCCESS) {
			print(response, Msg.SUCCESS);
		} else 
			print(response, Msg.ERROR);
	}
	

	@Autowired
	public void setMobileTaskDAO(MobileTaskDAO mobileTaskDAO) {
		this.mobileTaskDAO = mobileTaskDAO;
	}

	@Autowired
	public void setUserInfoDAO(UserInfoDAO userInfoDAO) {
		this.userInfoDAO = userInfoDAO;
	}
	
	
}
